package top.xiaoshuaila.moments.mapper;

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import top.xiaoshuaila.moments.pojo.Dynamic;
import top.xiaoshuaila.moments.pojo.RespBean;
import top.xiaoshuaila.moments.pojo.Visitor;

import java.util.List;

@Repository
public interface VisitorMapper {

    @Select("select \n" +
            "  *,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where users.id = focus.userid) as focus,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where users.id = focus.followerid) as focuto,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where focus.userid = ${vid}  \n" +
            "    and focus.followerid = ${id} ) > 0 as isfocus,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    blacklist \n" +
            "  where blacklist.userid = ${id}  \n" +
            "    and blacklist.blacklistid = ${vid} ) > 0 as ismyblacklist, " +
            "\n" +
            "(select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    blacklist \n" +
            "  where blacklist.blacklistid = ${id} \n" +
            "    and blacklist.userid = ${vid} ) > 0 as myisblacklist "+
            "from\n" +
            "  users \n" +
            "where users.id = ${vid} ;\n")
    Visitor getVisitById(long vid, long id);
    @Select("select \n" +
            "  a.`id` id,\n" +
            "  a.`userid` userid,\n" +
            "  a.`time` time,\n" +
            "  a.`iszhuan` iszhuan,\n" +
            "  a.`dyid` dyid,\n" +
            "  a.`selfonly` selfonly,\n" +
            "  b.`text` text,\n" +
            "  b.`title` title,\n" +
            "  b.`image` image,\n" +
            "  b.`userid` dyuserid,\n" +
            "  c.num zan,\n" +
            "  d.zhuan zhuan,\n" +
            "  e.`realname` realname,\n" +
            "  (select count(*)>0 from zan where zan.userid=${id} and zan.dyid=a.id) iszan\n" +
            "from\n" +
            "  userdy a \n" +
            "  left join dynamic b \n" +
            "    on a.`dyid` = b.`id` \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      count(*) as num \n" +
            "    from\n" +
            "      zan \n" +
            "    group by zan.`dyid`) c \n" +
            "    on a.id = c.dyid \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      count(*)-1 as zhuan \n" +
            "    from\n" +
            "      userdy \n" +
            "    group by userdy.`dyid`) d \n" +
            "    on b.id = d.dyid \n" +
            "  left join \n" +
            "    users e \n" +
            "    on b.`userid` = e.`id` \n" +
            "where a.userid = ${vid} and a.selfonly=0 order by a.time desc limit ${page},${num}")
    List<Dynamic> getdynamic(long id,long vid,int page,int num);
    @Select("select \n" +
            "  count(*)\n" +
            "from\n" +
            "  userdy a \n" +
            "where a.userid = ${vid} and a.selfonly=0")
    int gettotal(long vid);
}
